home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 October: Mac OS SDK / Dev.CD Oct 00 SDK1.toast / Development Kits / Mac OS / Appearance SDK 1.0.4 / Appearance Sample Code / Source / RadioGroup.cp < prev    next >
Encoding:
Text File  |  1999-07-16  |  3.3 KB  |  186 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        RadioGroup.cp
  3.  
  4.     Contains:    Class to implement a radio group.
  5.  
  6.     Version:    Appearance 1.0 SDK
  7.  
  8.     Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     File Ownership:
  11.  
  12.         DRI:                Edward Voas
  13.  
  14.         Other Contact:        7 of 9, Borg Collective
  15.  
  16.         Technology:            OS Technologies Group
  17.  
  18.     Writers:
  19.  
  20.         (edv)    Ed Voas
  21.  
  22.     Change History (most recent first):
  23.  
  24.          <1>     9/11/97    edv        First checked in.
  25. */
  26.  
  27. #include "RadioGroup.h"
  28.  
  29. typedef struct
  30. {
  31.     ControlRef        control;
  32.     SInt16            value;
  33. } RadioEntry;
  34.  
  35. RadioGroup::RadioGroup()
  36. {
  37.     fInfo = NewHandle( 0 );
  38.     fValue = 0;
  39.     fMixed = false;
  40. }
  41.  
  42. RadioGroup::~RadioGroup()
  43. {
  44.     DisposeHandle( fInfo );
  45. }
  46.         
  47. void
  48. RadioGroup::AddControl( ControlRef control, SInt16 value )
  49. {
  50.     Size        size;
  51.     SInt16        numItems;
  52.     
  53.     size = GetHandleSize( fInfo );
  54.     numItems = size / sizeof( RadioEntry );
  55.     
  56.     SetHandleSize( fInfo, size + sizeof( RadioEntry ) );
  57.     
  58.     ((RadioEntry*)*fInfo)[ numItems ].value = value;
  59.     ((RadioEntry*)*fInfo)[ numItems ].control = control;
  60.     
  61.     SetValue( value ); 
  62. }
  63.  
  64. SInt16
  65. RadioGroup::GetValue()
  66. {
  67.     return fValue;
  68. }
  69.  
  70. void
  71. RadioGroup::SetValue( SInt16 value )
  72. {
  73.     SInt16        numItems;
  74.     SInt16        i;
  75.     ControlRef    oldControl = nil, newControl = nil;
  76.     
  77.     if ( fMixed )
  78.     {
  79.         numItems = GetHandleSize( fInfo ) / sizeof( RadioEntry );
  80.         
  81.         for ( i = 0; i < numItems; i++ )
  82.         {
  83.             if ( GetControlValue( ((RadioEntry*)*fInfo)[ i ].control ) )
  84.             {
  85.                 SetControlValue( ((RadioEntry*)*fInfo)[ i ].control, 0 );
  86.             }
  87.         }
  88.     }
  89.     
  90.     if ( (value != fValue) || fMixed )
  91.     {
  92.         numItems = GetHandleSize( fInfo ) / sizeof( RadioEntry );
  93.         
  94.         for ( i = 0; i < numItems; i++ )
  95.         {
  96.             if ( ((RadioEntry*)*fInfo)[ i ].value == value )
  97.                 newControl = ((RadioEntry*)*fInfo)[ i ].control;
  98.             
  99.             if ( ((RadioEntry*)*fInfo)[ i ].value == fValue )
  100.                 oldControl = ((RadioEntry*)*fInfo)[ i ].control;
  101.         }
  102.         if ( newControl )
  103.         {
  104.             if ( oldControl )
  105.                 SetControlValue( oldControl, 0 );
  106.         
  107.             SetControlValue( newControl, 1 );
  108.         }
  109.         fValue = value;
  110.     }
  111.     fMixed = false;
  112. }
  113.  
  114. void
  115. RadioGroup::SetMixed( SInt16 value )
  116. {
  117.     SInt16        numItems;
  118.     SInt16        i;
  119.     ControlRef    newControl = nil;
  120.     
  121.     numItems = GetHandleSize( fInfo ) / sizeof( RadioEntry );
  122.     
  123.     for ( i = 0; i < numItems; i++ )
  124.     {
  125.         if ( ((RadioEntry*)*fInfo)[ i ].value == value )
  126.         {
  127.             newControl = ((RadioEntry*)*fInfo)[ i ].control;
  128.             break;
  129.         }
  130.     }
  131.     if ( newControl )
  132.     {
  133.         SetControlValue( newControl, kControlCheckBoxMixedValue );
  134.     }
  135.     fValue = value;
  136.  
  137.     fMixed = true;
  138. }
  139.  
  140. void
  141. RadioGroup::SetValueByControl( ControlRef control )
  142. {
  143.     SInt16        numItems;
  144.     SInt16        i;
  145.     ControlRef    oldControl = nil, newControl = nil;
  146.     SInt16        value;
  147.     Boolean        valueFound = false;
  148.         
  149.     if ( fMixed )
  150.     {
  151.         numItems = GetHandleSize( fInfo ) / sizeof( RadioEntry );
  152.         
  153.         for ( i = 0; i < numItems; i++ )
  154.         {
  155.             if ( GetControlValue( ((RadioEntry*)*fInfo)[ i ].control ) )
  156.             {
  157.                 SetControlValue( ((RadioEntry*)*fInfo)[ i ].control, 0 );
  158.             }
  159.         }
  160.     }
  161.     
  162.     numItems = GetHandleSize( fInfo ) / sizeof( RadioEntry );
  163.     
  164.     for ( i = 0; i < numItems; i++ )
  165.     {
  166.         if ( ((RadioEntry*)*fInfo)[ i ].control == control )
  167.         {
  168.             value = ((RadioEntry*)*fInfo)[ i ].value;
  169.             valueFound = true;
  170.         }
  171.         
  172.         if ( ((RadioEntry*)*fInfo)[ i ].value == fValue )
  173.             oldControl = ((RadioEntry*)*fInfo)[ i ].control;
  174.     }
  175.     if ( valueFound )
  176.     {
  177.         if ( oldControl )
  178.             SetControlValue( oldControl, 0 );
  179.     
  180.         SetControlValue( control, 1 );
  181.     
  182.         fValue = value;
  183.     }
  184.     fMixed = false;
  185. }
  186.